package com.qili.service.supervisor.impl;

import com.qili.base.mapper.BaseMapper;
import com.qili.base.service.impl.BaseServiceImpl;
import com.qili.entity.base.BaseDict;
import com.qili.entity.supervisor.ProjectContract;
import com.qili.entity.supervisor.ProjectUnitConstruction;
import com.qili.entity.supervisor.SupervisorFile;
import com.qili.entity.supervisor.dto.ProjectContractDTO;
import com.qili.entity.supervisor.dto.SupervisorProjectDTO;
import com.qili.mapper.supervisor.ProjectContractMapper;
import com.qili.service.base.BaseDictService;
import com.qili.service.supervisor.ProjectContractService;
import com.qili.service.supervisor.ProjectUnitConstructionService;
import com.qili.service.supervisor.SupervisorFileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by lenovo on 2020/11/4.
 */
@Slf4j
@Service
public class ProjectContractServiceImpl extends BaseServiceImpl<ProjectContract, String> implements ProjectContractService {

    @Autowired
    private SupervisorFileService supervisorFileService;
    @Autowired
    ProjectContractMapper projectContractMapper;
    @Autowired
    private BaseDictService baseDictService;
    @Autowired
    private ProjectUnitConstructionService unitConstructionService;

    @Override
    public BaseMapper<ProjectContract, String> getMappser() {
        return projectContractMapper;

    }

    @Override
    public int insert(ProjectContract projectContract) {
        return projectContractMapper.insert(projectContract);
    }

    @Override
    public boolean checkFileAtLeastOne(SupervisorProjectDTO supervisorProjectDTO) {
        if(StringUtils.isNotBlank(supervisorProjectDTO.getFbId())){
            // 到货验收
            //根据分包查询的:
            SupervisorFile supervisorFile = new SupervisorFile();
            supervisorFile.setType(supervisorProjectDTO.getType());
            supervisorFile.setProjectId(supervisorProjectDTO.getProjectId());
            supervisorFile.setSpmId(supervisorProjectDTO.getSpmId());
            supervisorFile.setPcId(supervisorProjectDTO.getFbId());

            ProjectContract projectContract = selectByPrimaryKey(supervisorProjectDTO.getFbId());

            //查询是否有对应的承建单位:
            ProjectUnitConstruction projectUnitConstruction = new ProjectUnitConstruction();
            projectUnitConstruction.setSupervisorProjectId(supervisorProjectDTO.getProjectId());
            projectUnitConstruction.setConstructionSubContractNumber(projectContract.getCode());
            List<ProjectUnitConstruction> select = unitConstructionService.select(projectUnitConstruction);

            List<SupervisorFile> listfb = supervisorFileService.select(supervisorFile);
            return ((select!=null && select.size()>0 && select.get(0)!=null) && (listfb != null && listfb.size() > 0));
        }else{
            // 20210707 非到货验收的分包校验  修改为 ：如果是多个分包 只要有一个分包上传了文件 就可以点击完成

            if("064".equals(supervisorProjectDTO.getType())){
                // 货物清单的校验
                // 查询分包表
                ProjectContract projectContract = new ProjectContract();
                projectContract.setProjectId(supervisorProjectDTO.getProjectId());
                List<ProjectContract> listcjht_fb = select(projectContract);
                //遍历分包:
                if (listcjht_fb.size() > 0) {
                    for (ProjectContract pc : listcjht_fb) {
                        SupervisorFile supervisorFile = new SupervisorFile();
                        supervisorFile.setType(supervisorProjectDTO.getType());// 城建合同 006
                        supervisorFile.setProjectId(supervisorProjectDTO.getProjectId());// 项目id
                        supervisorFile.setSpmId(supervisorProjectDTO.getSpmId());// 里程id
                        supervisorFile.setPcId(pc.getId());// 分包id
                        List<SupervisorFile> listfb = supervisorFileService.select(supervisorFile);

                        //查询是否有对应的承建单位:
                        ProjectUnitConstruction projectUnitConstruction = new ProjectUnitConstruction();
                        projectUnitConstruction.setSupervisorProjectId(supervisorProjectDTO.getProjectId());
                        projectUnitConstruction.setConstructionSubContractNumber(pc.getCode());
                        projectUnitConstruction.setInspectionMerchandise("1");
                        List<ProjectUnitConstruction> select = unitConstructionService.select(projectUnitConstruction);

                        if ((select!=null && select.size()>0 && select.get(0)!=null) && (listfb == null || listfb.size() == 0) ) {
                            return false;
                        }
                    }
                }

            }else{
                SupervisorFile checkFile = new SupervisorFile();
                checkFile.setType(supervisorProjectDTO.getType());
                checkFile.setProjectId(supervisorProjectDTO.getProjectId());
                checkFile.setSpmId(supervisorProjectDTO.getSpmId());
                int countfile = supervisorFileService.selectCount(checkFile);
                if (countfile == 0) {
                    return false;
                }

            }



            return true;
        }
    }

    @Override
    public boolean checkFileNecessary(SupervisorProjectDTO supervisorProjectDTO) {
        BaseDict wjBaci = new BaseDict();
        wjBaci.setBadtTypecode("WJ001");
        wjBaci.setBadtCode(supervisorProjectDTO.getType());
        List<BaseDict> select = baseDictService.select(wjBaci);
        if(select == null || select.size() == 0){
            log.warn("判断某文件是否是必选时,没有找到字典项",supervisorProjectDTO.getType());
        }
        BaseDict fileDict = select.get(0);
        return (StringUtils.isNotBlank(fileDict.getBadtLevel5()) && "1".equals(fileDict.getBadtLevel5()));
    }

    @Override
    public List<ProjectContract> selectPackageWithUnit(ProjectContract projectContract) {
        return projectContractMapper.selectPackageWithUnit(projectContract);
    }

    @Override
    public int deleteByProjectIds(String[] ids) {
        return projectContractMapper.deleteByProjectIds(ids);
    }

    @Override
    public List<ProjectContract> getProjectPackageByRole(ProjectContract projectContract) {
        return projectContractMapper.getProjectPackageByRole(projectContract);
    }
    @Override
    public List<ProjectContract> selectPackge(ProjectContractDTO projectContract){
        return projectContractMapper.selectPackge(projectContract);
    }

    @Override
    public int selectOrderMax(String id) {
        return projectContractMapper.selectOrderMax(id);
    }
}
